#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class VoteRecordProvider : CY.GFive.Core.Providers.DALProvider.IVoteRecordProvider
	{
		#region Sql
		private static readonly string SqlSelectVoteRecord = "SELECT [Id], [VoteRecordID],[UserID],[IP],[VoteTime],[Additional] FROM [VoteRecord] WHERE  [Id]=@Id";

		private static readonly string SqlInsertVoteRecord = "INSERT INTO [VoteRecord]([VoteRecordID],[UserID],[IP],[VoteTime],[Additional]) values(@VoteRecordID,@UserID,@IP,@VoteTime,@Additional);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateVoteRecord = "UPDATE [VoteRecord] SET [VoteRecordID] = @VoteRecordID,[UserID] = @UserID,[IP] = @IP,[VoteTime] = @VoteTime,[Additional] = @Additional WHERE  [Id]=@Id";

		private static readonly string SqlDeleteVoteRecord = "DELETE [VoteRecord] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllVoteRecord = "SELECT [Id], [VoteRecordID],[UserID],[IP],[VoteTime],[Additional] FROM [VoteRecord]";
		#endregion

		#region IVoteRecordProvider Members
		public Core.Business.VoteRecord Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectVoteRecord);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.VoteRecord voteRecord = new Core.Business.VoteRecord();

				if (!reader.IsDBNull(0)) voteRecord.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) voteRecord.VoteRecordID = reader.GetInt32(1);
				if (!reader.IsDBNull(2)) voteRecord.UserID = reader.GetInt32(2);
				if (!reader.IsDBNull(3)) voteRecord.IP = reader.GetString(3);
				if (!reader.IsDBNull(4)) voteRecord.VoteTime = reader.GetDateTime(4);
				if (!reader.IsDBNull(5)) voteRecord.Additional = reader.GetString(5);

				return voteRecord;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.VoteRecord voteRecord)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@VoteRecordID", SqlDbType.Int, voteRecord.VoteRecordID);
			sql.AddParameter("@UserID", SqlDbType.Int, voteRecord.UserID);
			sql.AddParameter("@IP", SqlDbType.NVarChar, voteRecord.IP);
			sql.AddParameter("@VoteTime", SqlDbType.DateTime, voteRecord.VoteTime);
			sql.AddParameter("@Additional", SqlDbType.NVarChar, voteRecord.Additional);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertVoteRecord);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) voteRecord.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.VoteRecord voteRecord)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@VoteRecordID", SqlDbType.Int, voteRecord.VoteRecordID);
			sql.AddParameter("@UserID", SqlDbType.Int, voteRecord.UserID);
			sql.AddParameter("@IP", SqlDbType.NVarChar, voteRecord.IP);
			sql.AddParameter("@VoteTime", SqlDbType.DateTime, voteRecord.VoteTime);
			sql.AddParameter("@Additional", SqlDbType.NVarChar, voteRecord.Additional);
			sql.AddParameter("@Id", SqlDbType.Int, voteRecord.Id);
			sql.ExecuteSql(SqlUpdateVoteRecord);
		}

		public void Delete(Core.Business.VoteRecord voteRecord)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, voteRecord.Id);
			sql.ExecuteSql(SqlDeleteVoteRecord);
		}

		public IList<Core.Business.VoteRecord> GetAllVoteRecord()
		{
			IList<Core.Business.VoteRecord> voteRecordlist = new List<Core.Business.VoteRecord>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllVoteRecord);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.VoteRecord voteRecord = new Core.Business.VoteRecord();

					if (!reader.IsDBNull(0)) voteRecord.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) voteRecord.VoteRecordID = reader.GetInt32(1);
					if (!reader.IsDBNull(2)) voteRecord.UserID = reader.GetInt32(2);
					if (!reader.IsDBNull(3)) voteRecord.IP = reader.GetString(3);
					if (!reader.IsDBNull(4)) voteRecord.VoteTime = reader.GetDateTime(4);
					if (!reader.IsDBNull(5)) voteRecord.Additional = reader.GetString(5);

					voteRecord.MarkOld();
					voteRecordlist.Add(voteRecord);
				}
				reader.Close();
			}
			return voteRecordlist;
		}
		#endregion
	}
}
